﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ContenutiDinamiciBLL;

namespace ContenutiDinamiciDAL
{
    public class ContenutiDinamiciController
    {
        ContenutiDinamiciModel.ContenutiDinamiciEntities m_ctx = new ContenutiDinamiciModel.ContenutiDinamiciEntities();

        public ContenutiDinamiciController()
        {

        }

        /// <summary>
        /// Gets the contexts.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Context> GetContexts()
        {
            IEnumerable<Context> retValue = null;

            var obj = from x in m_ctx.link_contexts
                      group x by x.site_code into g
                      select new Context { SiteCode = g.Key, ID=g.FirstOrDefault().id_link_context };

            if (obj.Any())
            {
                retValue = obj.ToList();
            }

            return retValue;
        }

        /// <summary>
        /// Gets the context.
        /// </summary>
        /// <param name="siteCode">The site code.</param>
        /// <returns></returns>
        public IEnumerable<Context> GetContext(string siteCode)
        {
            IEnumerable<Context> retValue = null;

            var obj = from x in m_ctx.link_contexts 
                      where x.site_code == siteCode
                      select new Context { 
                          ID = x.id_link_context,
                          Name = x.name,
                          Description = x.description
                      };

            if (obj.Any())
            {
                retValue = obj.ToList();
            }

            return retValue;
        }

        /// <summary>
        /// Gets the context details.
        /// </summary>
        /// <param name="contextId">The context id.</param>
        /// <returns></returns>
        public IEnumerable<Detail> GetContextDetails(string contextId)
        {
            IEnumerable<Detail> retValue = null;

            Guid id = Guid.Parse(contextId);

            var obj = from x in m_ctx.link_details
                      where x.id_link_context == id
                      select new Detail
                      {
                          ID = x.id_link_context,
                          Description = x.description,
                          Enabled = x.flag_enabled,
                          Order = x.order,
                          Context = new Context { ID = x.link_contexts.id_link_context },
                          DetailInfo = new HtmlDetailInfo { Css = x.detail_css, Html = x.detail_html, Image = x.img_src, Link = x.a_href, Tag = x.detail_tag, Target = x.detail_target, Text = x.detail_text },
                          Validity = new ValidityRange { From = x.data_from, To = x.data_to }
                      };

            if (obj.Any())
            {
                retValue = obj.ToList();
            }

            return retValue;
        }
    }
}
